﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX
{
    public class Layer
    {
        public LayerManager Manager;
        public int Index;
        public Layer Next = null;

        public int LayerIndex
        {
            get
            {
                return Index;
            }
        }

        public int FirstComponent;
        public int OneAfterLastComponent;    /* one after the index to the last in the layer group */

        public Layer(LayerManager manager, int index)
        {
            Manager = manager;
            Index = index;
        }

        public void AddComponent(GameComponent component)
        {
            Manager.Game.Components.Insert(OneAfterLastComponent, component);
            Manager.Map(component, this);
            this.OneAfterLastComponent++;
            if (Next != null)
            {
                Next.IncreaseIndices();
            }

            DrawableGameComponent dgc = component as DrawableGameComponent;
            dgc.DrawOrder = LayerIndex;
        }

        public void OneRemoved()
        {
            this.OneAfterLastComponent--;
            if (Next != null)
            {
                Next.DecreaseIndices();
            }
        }

        public void IncreaseIndices()
        {
            FirstComponent++;
            OneAfterLastComponent++;
            if (Next != null)
            {
                Next.IncreaseIndices();
            }
        }

        public void DecreaseIndices()
        {
            FirstComponent--;
            OneAfterLastComponent--;
            if (Next != null)
            {
                Next.DecreaseIndices();
            }
        }
    }
}
